﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类t_Efficency_Employee。
    /// </summary>
    public class t_Efficency_Employee
    {
        public t_Efficency_Employee()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordID)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Efficency_Employee where RecordID=@RecordID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, RecordID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.t_Efficency_Employee model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Efficency_Employee(");
            strSql.Append("RecordID,CityID,CompanyID,businessdistrictID,DepartID,EmployeeID,City,Company,businessdistrict,Departs,Employee,BeginTime,EndTime,YearMonth,TransType,Position,ChangType,PeopleNum)");

            strSql.Append(" values (");
            strSql.Append("@RecordID,@CityID,@CompanyID,@businessdistrictID,@DepartID,@EmployeeID,@City,@Company,@businessdistrict,@Departs,@Employee,@BeginTime,@EndTime,@YearMonth,@TransType,@Position,@ChangType,@PeopleNum)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, model.RecordID);
            db.AddInParameter(dbCommand, "CityID", DbType.Guid, model.CityID);
            db.AddInParameter(dbCommand, "CompanyID", DbType.Guid, model.CompanyID);
            db.AddInParameter(dbCommand, "businessdistrictID", DbType.Guid, model.businessdistrictID);
            db.AddInParameter(dbCommand, "DepartID", DbType.Guid, model.DepartID);
            db.AddInParameter(dbCommand, "EmployeeID", DbType.Guid, model.EmployeeID);
            db.AddInParameter(dbCommand, "City", DbType.AnsiString, model.City);
            db.AddInParameter(dbCommand, "Company", DbType.AnsiString, model.Company);
            db.AddInParameter(dbCommand, "businessdistrict", DbType.AnsiString, model.businessdistrict);
            db.AddInParameter(dbCommand, "Departs", DbType.AnsiString, model.Departs);
            db.AddInParameter(dbCommand, "Employee", DbType.AnsiString, model.Employee);
            db.AddInParameter(dbCommand, "BeginTime", DbType.AnsiString, model.BeginTime);
            db.AddInParameter(dbCommand, "EndTime", DbType.AnsiString, model.EndTime);
            db.AddInParameter(dbCommand, "YearMonth", DbType.AnsiString, model.YearMonth);
            db.AddInParameter(dbCommand, "TransType", DbType.AnsiString, model.TransType);
            db.AddInParameter(dbCommand, "Position", DbType.AnsiString, model.Position);
            db.AddInParameter(dbCommand, "ChangType", DbType.Int32, model.ChangType);
            db.AddInParameter(dbCommand, "PeopleNum", DbType.Decimal, model.PeopleNum);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.t_Efficency_Employee model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Efficency_Employee set ");
            strSql.Append("CityID=@CityID,");
            strSql.Append("CompanyID=@CompanyID,");
            strSql.Append("businessdistrictID=@businessdistrictID,");
            strSql.Append("DepartID=@DepartID,");
            strSql.Append("EmployeeID=@EmployeeID,");
            strSql.Append("City=@City,");
            strSql.Append("Company=@Company,");
            strSql.Append("businessdistrict=@businessdistrict,");
            strSql.Append("Departs=@Departs,");
            strSql.Append("Employee=@Employee,");
            strSql.Append("BeginTime=@BeginTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("YearMonth=@YearMonth,");
            strSql.Append("TransType=@TransType,");
            strSql.Append("Position=@Position,");
            strSql.Append("ChangType=@ChangType,");
            strSql.Append("PeopleNum=@PeopleNum");
            strSql.Append(" where RecordID=@RecordID ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, model.RecordID);
            db.AddInParameter(dbCommand, "CityID", DbType.Guid, model.CityID);
            db.AddInParameter(dbCommand, "CompanyID", DbType.Guid, model.CompanyID);
            db.AddInParameter(dbCommand, "businessdistrictID", DbType.Guid, model.businessdistrictID);
            db.AddInParameter(dbCommand, "DepartID", DbType.Guid, model.DepartID);
            db.AddInParameter(dbCommand, "EmployeeID", DbType.Guid, model.EmployeeID);
            db.AddInParameter(dbCommand, "City", DbType.AnsiString, model.City);
            db.AddInParameter(dbCommand, "Company", DbType.AnsiString, model.Company);
            db.AddInParameter(dbCommand, "businessdistrict", DbType.AnsiString, model.businessdistrict);
            db.AddInParameter(dbCommand, "Departs", DbType.AnsiString, model.Departs);
            db.AddInParameter(dbCommand, "Employee", DbType.AnsiString, model.Employee);
            db.AddInParameter(dbCommand, "BeginTime", DbType.AnsiString, model.BeginTime);
            db.AddInParameter(dbCommand, "EndTime", DbType.AnsiString, model.EndTime);
            db.AddInParameter(dbCommand, "YearMonth", DbType.AnsiString, model.YearMonth);
            db.AddInParameter(dbCommand, "TransType", DbType.AnsiString, model.TransType);
            db.AddInParameter(dbCommand, "Position", DbType.AnsiString, model.Position);
            db.AddInParameter(dbCommand, "ChangType", DbType.Int32, model.ChangType);
            db.AddInParameter(dbCommand, "PeopleNum", DbType.Decimal, model.PeopleNum);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Efficency_Employee ");
            strSql.Append(" where RecordID=@RecordID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, RecordID);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.t_Efficency_Employee GetModel(Guid RecordID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordID,CityID,CompanyID,businessdistrictID,DepartID,EmployeeID,City,Company,businessdistrict,Departs,Employee,dateadd(dd,1,BeginTime) as BeginTime,dateadd(dd,0,EndTime) as EndTime,YearMonth,TransType,Position,ChangType,PeopleNum from t_Efficency_Employee ");
            strSql.Append(" where RecordID=@RecordID ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, RecordID);
            Chenxin.Lib.Hr.Entity.t_Efficency_Employee model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordID,CityID,CompanyID,businessdistrictID,DepartID,EmployeeID,City,Company,businessdistrict,Departs,Employee,BeginTime,EndTime,YearMonth,TransType,Position,ChangType,PeopleNum ");
            strSql.Append(" FROM t_Efficency_Employee ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public static DataSet GetExportList(string strSql)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string GroupList, string FieldList, string TableFieldList, bool DoCount)
        //public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTableGroup");
            //DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "GroupList", DbType.AnsiString, GroupList);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetEmpList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string GroupList, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTableGroup");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "GroupList", DbType.AnsiString, GroupList);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.t_Efficency_Employee> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordID,CityID,CompanyID,businessdistrictID,DepartID,EmployeeID,City,Company,businessdistrict,Departs,Employee,BeginTime,EndTime,YearMonth,TransType,Position,ChangType,PeopleNum ");
            strSql.Append(" FROM t_Efficency_Employee ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.t_Efficency_Employee> list = new List<Chenxin.Lib.Hr.Entity.t_Efficency_Employee>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        static public DataSet Proce(Guid CityID, Guid CompanyID, string YearMonth, string BeginDay, string EndDay, decimal DayNum)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("P_GetPeopleNum");
            db.AddInParameter(dbCommand, "CityID", DbType.Guid, CityID);
            db.AddInParameter(dbCommand, "CompanyID", DbType.Guid, CompanyID);
            db.AddInParameter(dbCommand, "YearMonth", DbType.AnsiString, YearMonth);
            db.AddInParameter(dbCommand, "BeginTime", DbType.DateTime, BeginDay);
            db.AddInParameter(dbCommand, "EndTime", DbType.DateTime, EndDay);
            db.AddInParameter(dbCommand, "MonthDay", DbType.Decimal, DayNum);
            return db.ExecuteDataSet(dbCommand);
        }

        static public DataSet EProce(Guid CityID, Guid CompanyID, string YearMonth, string BeginDay, string EndDay, decimal DayNum)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("P_GetEmpNum");
            db.AddInParameter(dbCommand, "CityID", DbType.Guid, CityID);
            db.AddInParameter(dbCommand, "CompanyID", DbType.Guid, CompanyID);
            db.AddInParameter(dbCommand, "YearMonth", DbType.AnsiString, YearMonth);
            db.AddInParameter(dbCommand, "BeginTime", DbType.DateTime, BeginDay);
            db.AddInParameter(dbCommand, "EndTime", DbType.DateTime, EndDay);
            db.AddInParameter(dbCommand, "MonthDay", DbType.Decimal, DayNum);
            return db.ExecuteDataSet(dbCommand);
        }

        static public DataSet HProce(Guid CityID, Guid CompanyID, string YearMonth, string BeginDay, string EndDay, decimal DayNum)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("P_GetHalfShoper");
            db.AddInParameter(dbCommand, "CityID", DbType.Guid, CityID);
            db.AddInParameter(dbCommand, "CompanyID", DbType.Guid, CompanyID);
            db.AddInParameter(dbCommand, "YearMonth", DbType.AnsiString, YearMonth);
            db.AddInParameter(dbCommand, "BeginTime", DbType.DateTime, BeginDay);
            db.AddInParameter(dbCommand, "EndTime", DbType.DateTime, EndDay);
            db.AddInParameter(dbCommand, "MonthDay", DbType.Decimal, DayNum);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.t_Efficency_Employee ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.t_Efficency_Employee model = new Chenxin.Lib.Hr.Entity.t_Efficency_Employee();
            object ojb;
            ojb = dataReader["RecordID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordID = new Guid(ojb.ToString());
            }
            ojb = dataReader["CityID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityID = new Guid(ojb.ToString());
            }
            ojb = dataReader["CompanyID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyID = new Guid(ojb.ToString());
            }
            ojb = dataReader["businessdistrictID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.businessdistrictID = new Guid(ojb.ToString());
            }
            ojb = dataReader["DepartID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartID = new Guid(ojb.ToString());
            }
            ojb = dataReader["EmployeeID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.EmployeeID = new Guid(ojb.ToString());
            }
            model.City = dataReader["City"].ToString();
            model.Company = dataReader["Company"].ToString();
            model.businessdistrict = dataReader["businessdistrict"].ToString();
            model.Departs = dataReader["Departs"].ToString();
            model.Employee = dataReader["Employee"].ToString();
            model.BeginTime = dataReader["BeginTime"].ToString();
            model.EndTime = dataReader["EndTime"].ToString();
            model.YearMonth = dataReader["YearMonth"].ToString();
            model.TransType = dataReader["TransType"].ToString();
            model.Position = dataReader["Position"].ToString();
            ojb = dataReader["ChangType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ChangType = (int)ojb;
            }
            ojb = dataReader["PeopleNum"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.PeopleNum = (decimal)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

